/**
 * ***************************************************************************
 * Define Drag & Drop Mangaer.
 * @author ChenHang
 * @version 1.0.0 2008/10/12 (Standardized)
 * <PRE>
 * [Revision History]
 * 2008/10/12    ver1.0.0    Creation        ChenHang
 * </PRE>
 * Copyright 2008 USI, All rights reserved.
 * ***************************************************************************
 */
var DDConstants = {
	//SearchFolder
	SEARCH_FOLDER: "_SearchFolder",
	MASTER_TAG: "_MasterTag",
	FREE_TAG: "_FreeTag",
	DOCUMENT: "_Document",
	SPACE_FOLDER: "_SpaceFolder",
	SPACE_SYSTEM_FOLDER: "_SpaceSystemFolder",
	SPACE_LINK: "_SpaceLink",
	SPACE_MASTER_TAG: "_SpaceMasterTag",
	SPACE_FREE_TAG: "_SpaceFreeTag",
	DEFAULT_GROUP:["default"]
};

/**
*Customized DDProxy Class
*/
var InsightDD = function(id, sGroup, config, metaInfo) { 
	InsightDD.superclass.constructor.call(this, id, sGroup, config);
	this.metaInfo = metaInfo; 
	this.logger = this.logger || YAHOO;
	this.isTarget = false; 
    var el = this.getDragEl();
    /*The proxy is slightly transparent*/
	YAHOO.util.Dom.setStyle(el, "opacity", 0.67);
};

YAHOO.extend(InsightDD, YAHOO.util.DDProxy, {
	startDrag: function(x, y) { 
		this.logger.log(this.id + " startDrag"); 
		// make the proxy look like the source element 
		var dragEl = this.getDragEl(); 
		var clickEl = this.getEl(); 
		//do not need to hide source element.
		//YAHOO.util.Dom.setStyle(clickEl, "visibility", "hidden"); 
		dragEl.innerHTML = clickEl.innerHTML; 
        YAHOO.util.Dom.setStyle(dragEl, "color", YAHOO.util.Dom.getStyle(clickEl, "color")); 
        YAHOO.util.Dom.setStyle(dragEl, "backgroundColor", YAHOO.util.Dom.getStyle(clickEl, "backgroundColor")); 
        //YAHOO.util.Dom.setStyle(dragEl, "border", "2px solid gray"); 
	},
	onMouseDown : function (event) {
		EventBus.publish(EVENT_MOUSE_DOWN_ON_DRAG, event);
	},
	
	endDrag: function(e) {
	},
	
	onDragEnter : function(e, id) {
		this.highlightTarget(e, id);
	},
	
	onDragOut : function(e, id) {
		this.clearTarget(e, id);
	},
	
	highlightTarget : function(e, id) {
		var ddTarget;
		var dom = YAHOO.util.Dom;
        if ("string" == typeof id) { 
            ddTarget = YAHOO.util.DDM.getDDById(id); 
        } else { 
            ddTarget = YAHOO.util.DDM.getBestMatch(id); 
        }
        var element = ddTarget.getEl();
        //element.originalBackgroundColor = dom.getStyle(element, "backgroundColor");
        element.originalBorderColor = dom.getStyle(element, "borderColor");
        element.originalBorderStyle = dom.getStyle(element, "borderStyle");
        element.originalBorderWidth = dom.getStyle(element, "borderWidth");
        //dom.setStyle(element, "borderColor",
        //		dom.getStyle(this.getDragEl(), "backgroundColor"));
        var bColor = ddTarget.borderColor?ddTarget.borderColor:"#E02040";
        var bStyle = ddTarget.borderStyle?ddTarget.borderStyle:"dashed";
        var bWidth = ddTarget.borderWidth?ddTarget.borderWidth:"2px";
        dom.setStyle(element, "borderColor", bColor);
        dom.setStyle(element, "borderStyle", bStyle);
        dom.setStyle(element, "borderWidth", bWidth);
        //this.targetElement = element;
	},
	
	clearTarget : function(e, id) {
		var ddTarget;
        if ("string" == typeof id) { 
            ddTarget = YAHOO.util.DDM.getDDById(id); 
        } else { 
            ddTarget = YAHOO.util.DDM.getBestMatch(id); 
        }
        var element = ddTarget.getEl();
        //element = this.targetElement;
        //dom.setStyle(element, "backgroundColor", (element.originalBackgroundColor) ? element.originalBackgroundColor : "");
        this.clearElementTargetStyle(element);
	},
	
	clearTargets : function(e, id) {
		for (var i = 0; i < id.length; i++) {
			this.clearElementTargetStyle(id[i].getEl());
		}
	},
	
	clearElementTargetStyle: function(element) {
		var dom = YAHOO.util.Dom;
        if (element.originalBorderColor) {
			dom.setStyle(element, "borderColor", element.originalBorderColor);        	
        } else {
        	dom.setStyle(element, "borderColor", "transparent");
        }
        if (element.originalBorderStyle) {
			dom.setStyle(element, "borderStyle", element.originalBorderStyle);        	
        } else {
        	dom.setStyle(element, "borderStyle", "none");
        }
        if (element.originalBorderWidth) {
			dom.setStyle(element, "borderWidth", element.originalBorderWidth);        	
        } else {
        	dom.setStyle(element, "borderWidth", "0px");
        }
	},
	
	onDragDrop: function(e, id) { 
		// get the drag and drop object that was targeted 
	    var ddTarget; 	        
        if ("string" == typeof id) { 
            ddTarget = YAHOO.util.DDM.getDDById(id);
            this.clearTarget(e, id); 
        } else {
           ddTarget = YAHOO.util.DDM.getBestMatch(id);
           this.clearTargets(e, id);
        }        
        for (iGroup in this.groups) {
        	for (tGroup in ddTarget.groups) {
        		if (iGroup == tGroup) {
        			ddTarget.callback(e, ddTarget.id, this.metaInfo, ddTarget);
        			//stop event popup.
        			return false;
        		}
        	}
        }
	} 	 
});


/**
* Drag & Drog Manager.
* Now only support DDProxy, not DD.
*/
var DDManager = Class.create();
DDManager.prototype=  
{
	/** @constructs */
	initialize : function(){
		this.componentId = "DDManager" + (new Date()).getTime();
	    /**widget definition id*/
		this.componentDefId = "DDManager";
	},
	
	/**
	* register Drag and Drop item to specified group with meta infomation.
	* if no specified group, then give ["default"].
	* you can set callback method on return object(InsightDD object).
	*/
	registerDDItem : function(elementId, groups, config, metaInfo){
		var ddItem = new InsightDD(elementId, groups[0], config, metaInfo);
		
		for (var i = 1; i < groups.length; i++) {
			ddItem.addToGroup(groups[i]);
		}
		return ddItem;
	},
	
	/**
	* register Drag and Drop item to specified group with meta infomation.
	* if no specified group, then give ["default"].
	* the signature of callback method is like below.
	* (e, id) : e <Event> the mouseup event, id <String|DragDrop[]> In POINT mode, the element id this was dropped on. In INTERSECT mode, an array of dd items this was dropped on. 
	*/
	registerDDContainer : function(elementId, groups, config, callback){
		var target = new YAHOO.util.DDTarget(elementId, groups[0], config);
		target.dragElId = "ddProxy";
		target.borderColor = config.borderColor;
		target.borderStyle = config.borderStyle;
		target.borderWidth = config.borderWidth;
		for (var i = 1; i < groups.length; i++) {
			target.addToGroup(groups[i]);
		}
		target.callback = callback;
		return target;
	}
};

var ddManager = new DDManager();
//YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
